package com.clkj.sunhelp.mvp.policyinfo.policyinfolist;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForPolicy;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.PolicyKindItem;
import com.clkj.sunhelp.bean.PolicyListItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.common.IntentKey;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.mvp.policyinfo.policydetail.ActivityPolicyDetail;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 政策法规类型选择（不同的局选择）
 * Created by Administrator on 2017/3/26.
 */

public class ActivityPolicyList extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, PolicyListContract.View {

    private PullToRefreshListView lv;
    private ListView originalLv;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;


    ListAdapterForPolicy mAdapter;
    List<PolicyListItem> mPolicyList = new ArrayList<>();


    Integer mPageIndex;
    boolean canLoad;
    boolean isRefresh;

    PolicyKindItem mPolicyKindItem;

    PolicyListContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy_list);

        initData();
        assignView();
        initView();

        mPresenter = new PolicyListPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

        getOriginalList();
    }

    @Override
    public void initData() {
        mPolicyKindItem = (PolicyKindItem) getIntent().getSerializableExtra(IntentKey.INTENT_KEY_POLICY_KIND);
      /*  PolicyListItem policyListItem = new PolicyListItem();
        policyListItem.setPolicyPublishTime("2017-02-03 10:30");
        policyListItem.setPolicyTitle("政策法规title政策法规title政策法规title");

        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
        mPolicyList.add(policyListItem);
*/


    }

    @Override
    public void assignView() {
        lv = (PullToRefreshListView) findViewById(R.id.lv);
        originalLv = lv.getRefreshableView();
        layoutEmpty = (RelativeLayout) findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) findViewById(R.id.tv_emptytip);
    }

    @Override
    public void initView() {
        initTitleWithRightTvOrIv(null, null, mPolicyKindItem.getItemName(), true, null, null);
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lv.setOnRefreshListener(this);

        mAdapter = new ListAdapterForPolicy(this, mPolicyList);
        originalLv.setAdapter(mAdapter);
        originalLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent toPolicyDetail = new Intent(ActivityPolicyList.this, ActivityPolicyDetail.class);
                toPolicyDetail.putExtra(IntentKey.INTENT_KEY_POLICY_ID, mPolicyList.get(i - 1).getId());
                startActivity(toPolicyDetail);
            }
        });
    }


    private void getOriginalList() {
        isRefresh = true;
        mPageIndex = 1;
        getPolicyList();
    }


    private void getMoreList() {
        if (canLoad) {
            mPageIndex++;
            getPolicyList();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        getOriginalList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        getMoreList();
    }

    @Override
    public void getPolicyList() {
        mPresenter.getPolicyList(mPolicyKindItem.getItemName(), mPageIndex.toString(), Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetPolicyListSuccess(List<PolicyListItem> policyList) {
        lv.onRefreshComplete();

        if (isRefresh) {
            mPolicyList.clear();
            isRefresh = false;
        }
        if (policyList.size() > 0) {
            if (policyList.size() >= Constant.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            mPolicyList.addAll(policyList);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (mPolicyList.size() == 0) {
                lv.setEmptyView(layoutEmpty);
            } else {
                ToastUtil.show(this, "已经获取全部数据", 2000);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetPolicyListError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(this, tip);
        if (isRefresh) {
            mPolicyList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(PolicyListContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }
}
